<template>
  <div style="margin-left: 12px">
    <div id="divSearch" ref="divSearch" style="margin-top: 30px">
      <el-form
        :inline="true"
        :model="Search"
        size="mini"
        class="demo-form-inline"
      >
        <el-form-item label="单位名称" prop="Search">
          <el-input v-model.trim="Search.Organizename" placeholder="名称" />
        </el-form-item>
        <el-form-item label="单位编码" prop="Search">
          <el-input v-model.trim="Search.Organizeid" placeholder="编码" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="getDataList"
            >查询</el-button
          >
          <el-button
            type="success"
            icon="el-icon-circle-plus"
            size="small"
            @click="handeAdd"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div style="width: 100%">
      <el-table
        :data="DataList"
        style="width: 100%; margin-top: 0px"
        border
        @sort-change="sortChange"
        :height="tableHeight"
      >
        <el-table-column align="center" label="编号">
          <template slot-scope="scope">{{ scope.row.ApprovalNo }}</template>
        </el-table-column>
        <el-table-column align="center" label="单位编码">
          <template slot-scope="scope">{{ scope.row.Organizeid }}</template>
        </el-table-column>

        <el-table-column align="center" label="单位名称">
          <template slot-scope="scope">{{ scope.row.Organizename }}</template>
        </el-table-column>
        <el-table-column align="center" label="是否启用">
          <template slot-scope="scope">{{ scope.row.Status }}</template>
        </el-table-column>
        <el-table-column align="center" label="修改人">
          <template slot-scope="scope">{{ scope.row.UserName }}</template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope)"
              >修改</el-button
            >
            <el-button type="danger" size="small" @click="handleDelete(scope)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      background
      layout="sizes, prev, pager, next,total"
      :page-size="Search.pageSize"
      :total="Search.total"
      @current-change="pageChange"
    />
    <el-form>
      <el-dialog :visible.sync="dialogVisible" width="1000px" top="20px">
        <el-tabs type="border-card">
          <el-tab-pane label="单位信息">
            <el-form
              :model="EditObj"
              :inline="true"
              label-width="120px"
              label-position="left"
            >
              <el-form-item label="编码" v-if="false">
                <el-input v-model="EditObj.ApprovalNo" placeholder=" 编码" />
              </el-form-item>
              <el-form-item :model="form" prop="PaymentDate" label="单位编码">
                <el-input v-model="EditObj.Organizeid" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="openDept"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="单位名称">
                <el-input
                  v-model="EditObj.Organizename"
                  placeholder=" 单位名称"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="部门负责人工号">
                <el-input v-model="EditObj.DepartmentMan" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(1)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="部门负责人姓名">
                <el-input
                  v-model="EditObj.DepartmentManName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />
              
              <el-form-item :model="form" prop="PaymentDate" label="分公司综合部人员工号">
                <el-input v-model="EditObj.ExtApprovalCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(2)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="分公司综合部人员">
                <el-input
                  v-model="EditObj.ExtApprovalName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="分公司综合部部门负责人员工号">
                <el-input v-model="EditObj.ExtDepApprovalCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(3)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="分公司综合部部门负责人员">
                <el-input
                  v-model="EditObj.ExtDepApprovalName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="分公司单位财务部人员工号">
                <el-input v-model="EditObj.FinanceApprovalCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(4)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="分公司单位财务部人员">
                <el-input
                  v-model="EditObj.FinanceApprovalName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="招募中心人员工号">
                <el-input v-model="EditObj.HRApprovalCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(5)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="招募中心人员">
                <el-input
                  v-model="EditObj.HRApprovalName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="分公司单位负责人工号">
                <el-input v-model="EditObj.ExtCenterMan" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(6)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="分公司单位负责人">
                <el-input
                  v-model="EditObj.ExtCenterManName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="单位总经办人员工号">
                <el-input v-model="EditObj.ExtCenterGMOMan" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(7)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="单位总经办人员">
                <el-input
                  v-model="EditObj.ExtCenterGMOManName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="制造群群总办公室人员工号">
                <el-input v-model="EditObj.GMOApprovalCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(8)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="制造群群总办公室人员">
                <el-input
                  v-model="EditObj.GMOApprovalName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="制造群财务工号">
                <el-input v-model="EditObj.FinanceCentreApprovalCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(9)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="制造群财务人员">
                <el-input
                  v-model="EditObj.FinanceCentreApprovalName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="财务管理中心中心办工号">
                <el-input v-model="EditObj.FinanceManagementCenterOfficeCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(10)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="财务管理中心中心办人员">
                <el-input
                  v-model="EditObj.FinanceManagementCenterOfficeName"
                  placeholder="姓名"
                />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="常务副总裁工号">
                <el-input v-model="EditObj.CMOCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(11)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="常务副总裁">
                <el-input v-model="EditObj.CMOName" placeholder="姓名" />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="董事会">
                <el-input v-model="EditObj.CEOCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(12)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="董事会">
                <el-input v-model="EditObj.CEOName" placeholder="姓名" />
              </el-form-item>
              <br />

              <el-form-item :model="form" prop="PaymentDate" label="财务管理中心总监">
                <el-input v-model="EditObj.FinanceManagementCenterCode" :disabled="true">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="open(13)"
                  ></el-button>
                </el-input>
              </el-form-item>
              <el-form-item label="财务管理中心总监">
                <el-input v-model="EditObj.FinanceManagementCenterName" placeholder="姓名" />
              </el-form-item>
              <br />

              <el-form-item label="考勤类型">
                <el-select
                  v-model="EditObj.AttendenceType"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <br />
              <el-form-item label="是否启用">
                <el-select
                  v-model="EditObj.Status"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in optiontwo"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

        <div style="text-align: right">
          <el-button type="danger" @click="dialogVisible = false"
            >取消</el-button
          >
          <el-button type="primary" @click="confirmData">提交</el-button>
        </div>

        
      </el-dialog>
      <el-dialog
          v-if="dialogFormVisible"
          :visible.sync="dialogFormVisible"
          :title="人员资料"
          customClass="customWidth"
          width="60%"
        >
          <el-form :model="name" label-width="80px" label-position="left">
            <v-AccountOpen
              :ParmType="ParmType"
              @FunAccountAdd="FunAccountAdd"
            ></v-AccountOpen>
          </el-form>
      </el-dialog>
      <el-dialog
        v-if="dialogDeptVisible"
        :visible.sync="dialogDeptVisible"
        :title="单位资料"
        customClass="customWidth"
        width="60%"
      >
        <el-form :model="name" label-width="80px" label-position="left">
          <v-DepOpen @FunDepAdd="FunDepAdd"></v-DepOpen>
        </el-form>
      </el-dialog>
    </el-form>
  </div>
</template>

<script>
import path from "path";
import { deepClone } from "@/utils";
import {
  getDataList,
  updateOrganizeData,
  deletOrganizeData,
} from "@/api/System";
import elementIcons from "../icons/element-icons";
import vAccountOpen from '../Common/AccountOpen';
import vDepOpen from "../Common/DepOpen";
// 默认的编辑字段
const defaultObject = {};

export default {
  name: "Organize",

  components: {
    vAccountOpen,
    vDepOpen,
  },

  data() {
    return {
      tableHeight:null,
      isEdit: false,
      EditObj: [],
      DataList: [], //队列的列表 在el-table中用
      dialogVisible: false,
      dialogTableVisible: false,
      Search: {
        // 查询条件
        total: 0, // 总记录数
        pageSize: 10, // 一页显示的记录数
        page: 1, // /当前页码
        RNO: "003", // 表编号  根据Controller中对应的表编号
        jsWhere: "", // 自定义条件
      },
      options: [
        {
          value: 1,
          label: "一级MCAS",
        },
        {
          value: 2,
          label: "二级MCAS",
        },
        {
          value: 3,
          label: "人事系统",
        },
      ],
      optiontwo: [
        {
          value: 1,
          label: "否",
        },
        {
          value: 2,
          label: "是",
        },
      ],
      dialogFormVisible: false,
      dialogDeptVisible: false,
      multipleTable: [],
      DeptTable:[],
    };
  },
  computed: {
    routesData() {
      return this.routes;
    },
  },
  created() {
    // Mock: get all routes and roles list from server

    this.getDataList();
  },
  mounted() {
    setTimeout(() => {
      this.tableHeight = window.innerHeight - 220 + "px";
    });
  },
  methods: {
    pageChange(page) {
      // 翻页
      this.Search.page = page;

      this.getDataList();
    },
    async getDataList() {
      // 获取列表数据，通用
      const res = await getDataList({ data: JSON.stringify(this.Search) });
      var temp=res.data.Table
      temp.forEach((c) => {
        if(c.Status==2){
          c.Status='启用'
        }
        else{
          c.Status='禁用'
        }
      });
      this.DataList = temp; //将data 中的数据放到DataList中
      this.Search.total = res.data.Table1[0].RecordCount;
    },
    sortChange: function (column) {
      // column, prop, order,排序，通用

      this.Search.sort = column.prop + " " + column.order;
      this.getDataList();
    },
    async handeAdd() {
      // 新建
      this.dialogVisible = true;
      const result = await updateOrganizeData({
        BasicData: null,
        ApprovalNo: "-1",
        IsPostBack: false,
      });
      this.EditObj = result.data.Table[0];
      console.log(this.EditObj.ApprovalNo);
      if (this.EditObj.ApprovalNo) {
        this.isEdit = true;
      } else {
        this.isEdit = false;
      }
      if (this.$refs.tree) {
        this.$refs.tree.setCheckedNodes([]);
      }
      this.dialogType = "new";
    },
    async handleEdit(scope) {
      // 修改
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.checkStrictly = true;
      const result = await updateOrganizeData({
        BasicData: null,
        ApprovalNo: scope.row.ApprovalNo,
        IsPostBack: false,
      });
      this.EditObj = result.data.Table[0];
      if (this.EditObj.ApprovalNo) {
        this.isEdit = true;
      } else {
        this.isEdit = false;
      }
      this.$nextTick(() => {
        const routes = this.generateRoutes(this.EditObj.routes);
        this.$refs.tree.setCheckedNodes(this.generateArr(routes));
        this.checkStrictly = false;
      });
    },
    async handleDelete({ $index, row }) {
      //删除
      this.$confirm("确定删除该记录吗?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        await deletOrganizeData({
          ApprovalNo: JSON.stringify(row.ApprovalNo),
        });
        this.DataList.splice($index, 1);
        if (result.code === 20000) {
          this.dialogVisible = false;
          this.$notify({
            title: "Success",
            dangerouslyUseHTMLString: true,
            message: result.msg,
            type: "success",
          });
        } else {
          this.$notify({
            title: "error",
            dangerouslyUseHTMLString: true,
            message: result.msg,
            type: "error",
          });
        }
      });
    },
    async confirmData() {
      // 提交数据，需要修改
      const isEdit = this.dialogType === "edit";
      if (isEdit || true) {
        const result = await updateOrganizeData({
          BasicData: JSON.stringify(this.EditObj),
          ApprovalNo: this.EditObj.ApprovalNo,
          IsPostBack: true,
        });
        if (result.code === 20000) {
          const { description, key, name } = this.EditObj;
          this.dialogVisible = false;
          this.$notify({
            title: "Success",
            dangerouslyUseHTMLString: true,
            message: result.msg,
            type: "success",
          });
          this.getDataList();
        } else {
          this.$notify({
            title: "error",
            dangerouslyUseHTMLString: true,
            message: result.msg,
            type: "error",
          });
        }
      }
    },
    open(type) {
      this.ParmType = type; //传入参数
      this.dialogFormVisible = true;
    },
    FunAccountAdd: function (Rows, ParmType) {
      //添加部门

      this.multipleTable = Rows[0];

      if (ParmType == 1) {
        this.EditObj.DepartmentMan = this.multipleTable.Account;
        this.EditObj.DepartmentManName = this.multipleTable.Name;
      } else if (ParmType == 2) {
        this.EditObj.ExtApprovalCode = this.multipleTable.Account;
        this.EditObj.ExtApprovalName = this.multipleTable.Name;
      }
      else if (ParmType == 3) {
        this.EditObj.ExtDepApprovalCode = this.multipleTable.Account;
        this.EditObj.ExtDepApprovalName = this.multipleTable.Name;
      }
      else if (ParmType == 4) {
        this.EditObj.FinanceApprovalCode = this.multipleTable.Account;
        this.EditObj.FinanceApprovalName = this.multipleTable.Name;
      }
      else if (ParmType == 5) {
        this.EditObj.HRApprovalCode = this.multipleTable.Account;
        this.EditObj.HRApprovalName = this.multipleTable.Name;
      }
      else if (ParmType == 6) {
        this.EditObj.ExtCenterMan = this.multipleTable.Account;
        this.EditObj.ExtCenterManName = this.multipleTable.Name;
      }
      else if (ParmType == 7) {
        this.EditObj.ExtCenterGMOMan = this.multipleTable.Account;
        this.EditObj.ExtCenterGMOManName = this.multipleTable.Name;
      }
      else if (ParmType == 8) {
        this.EditObj.GMOApprovalCode = this.multipleTable.Account;
        this.EditObj.GMOApprovalName = this.multipleTable.Name;
      }
      else if (ParmType == 9) {
        this.EditObj.FinanceCentreApprovalCode = this.multipleTable.Account;
        this.EditObj.FinanceCentreApprovalName = this.multipleTable.Name;
      }
      else if (ParmType == 10) {
        this.EditObj.FinanceManagementCenterOfficeCode = this.multipleTable.Account;
        this.EditObj.FinanceManagementCenterOfficeName = this.multipleTable.Name;
      }
      else if (ParmType == 11) {
        this.EditObj.CMOCode = this.multipleTable.Account;
        this.EditObj.CMOName = this.multipleTable.Name;
      }
      else if (ParmType == 12) {
        this.EditObj.CEOCode = this.multipleTable.Account;
        this.EditObj.CEOName = this.multipleTable.Name;
      }
      else if (ParmType == 13) {
        this.EditObj.FinanceManagementCenterCode = this.multipleTable.Account;
        this.EditObj.FinanceManagementCenterName = this.multipleTable.Name;
      }

      this.dialogFormVisible = false;
    },
    //添加单位
    openDept(){
      this.dialogDeptVisible=true
    },
    FunDepAdd: function (Rows) {
      this.DeptTable=Rows[0]

      this.EditObj.Organizeid=this.DeptTable.organizeid
      this.EditObj.Organizename=this.DeptTable.organizename

      this.dialogDeptVisible = false;
    }
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  .EditObjs-table {
    margin-top: 30px;
  }

  .permission-tree {
    margin-bottom: 5px;
  }

  /* 解决element-ui的table表格控件表头与内容列不对齐问题 */
  .el-table {
    display: table-cell !important;
    text-align: center;
  }
}
.customWidth {
  width: 80%;
  height: 860px;
}
</style>
